package com.fbd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbd.common.R;
import com.fbd.dto.DishDto;
import com.fbd.entity.Category;
import com.fbd.entity.Dish;
import com.fbd.entity.DishFlavor;
import com.fbd.service.CategoryService;
import com.fbd.service.DishFlavorService;
import com.fbd.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){

        String key = "dish_" + dishDto.getCategoryId() +"_1";
        redisTemplate.delete(key);
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    private R<Page> page(int page,int pageSize,String name){
        //构造分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //构造条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name!=null,Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getUpdateTime);
        //执行查询
        dishService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();
        List<DishDto> lists = new ArrayList<>();
        for (Dish record :records){
            DishDto list = new DishDto();
            //对象拷贝
            BeanUtils.copyProperties(record,list);
            Long categoryId = record.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if (byId!=null){
                String categoryName = byId.getName();
                list.setCategoryName(categoryName);
            }
            lists.add(list);
        }
        dishDtoPage.setRecords(lists);
        return R.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){

        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);
        return R.success(byIdWithFlavor);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        String key = "dish_" + dishDto.getCategoryId() +"_1";
        redisTemplate.delete(key);

        dishService.updateByIdWithFlavor(dishDto);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> deleteById(Long ids){

        return null;
    }
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtos = null;
        String key = "dish_"+dish.getCategoryId()+"_"+dish.getStatus();
        dishDtos = (List<DishDto>) redisTemplate.opsForValue().get(key);
        if(dishDtos != null){
            return R.success(dishDtos);
        }

        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        //状态为启售的商品
        queryWrapper.eq(Dish::getStatus,1);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);
        dishDtos = new ArrayList<>();
        for (Dish dish1 : list){
            DishDto dishDto = new DishDto();
            //复制对象
            BeanUtils.copyProperties(dish1,dishDto);
            //获得菜品id
            Long id = dish1.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            //查询出此菜品的口味集合
            List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
            //将口味集合添加到菜品中
            dishDto.setFlavors(dishFlavorList);
            dishDtos.add(dishDto);
        }
        //如果redis中不存在，就将查询的数据存入redis中 60分钟后消失
        redisTemplate.opsForValue().set(key,dishDtos,60, TimeUnit.MINUTES);
        return R.success(dishDtos);
    }
}
